<?php
//自动生成,请根据需要修改
namespace app\order\controller;

use app\common\controller\Base;
use think\Db;
use tools\curl\Curl;

/**
 * @title 购物车（模块08-02）
 * @description 购物车信息说明
 * @group 订单（08）
 * @header name:model_id require:0 default:08-02 desc:模块
 * @groupremark 已使用错误码：
 */
class FrontCart extends Base
{

    /**
     * @title 添加商品到购物车
     * @description 接口说明
     * @author 李锐志
     * @url /order/front_cart/add_good
     * @method ~
     * @param name:token mobile type:int require:1 desc:token
     * @param name:good_id mobile type:int require:1 desc:商品id
     * @param name:sku_id mobile type:int require:1 desc:skuid
     * @param name:num mobile type:int require:1 desc:数量
     */
    public function add_good()
    {

        $data = input();

        $data['account_id'] = UID;

        //获取商品信息
        $good_model_api = Config('model_api')['good'];
        $url = $good_model_api . "/good/info";
        $good = Curl::get($url, array('id' => $data['good_id']));
        $good = Curl::json2arr($good);

        if (!isset($data['sku_id'])) {  //没有sku的情况

            //获取商品库存
            $good_num = $good['data']['num'];

            //获取购物车中商品的数量
            $num = Db::name('cart')
                ->where('account_id', $data['account_id'])
                ->where('good_id', $data['good_id'])
                ->value('num');

            //判断库存是否足够
            if ($good_num < $data['num'] || $data['num'] <= 0 || $good_num <= $num || $good_num < $num + $data['num']) {
                $msg['error_id'] = '1-08-02-0001';
                $msg['msg'] = '添加失败，库存不够或数量有误';
                return $this->errorJson($msg);
            }

            $data['good_orignal_price'] = $good['data']['original_price'];

            $data['good_price'] = $good['data']['total_sales_price'];

            $cart_good = Db::name('cart')
                ->field('num')
                ->where('account_id', $data['account_id'])
                ->where('good_id', $data['good_id'])
                ->find();

        } else {    //有sku的情况
            $url = $good_model_api . "/good_sku/info";
            $good_sku = Curl::get($url, array('id' => $data['sku_id'], 'good_id' => $data['good_id']));
            $good_sku = Curl::json2arr($good_sku);

            //获取商品库存
            $good_num = $good_sku['data']['num'];

            //获取购物车中商品的数量
            $num = Db::name('cart')
                ->where('account_id', $data['account_id'])
                ->where('sku_id', $data['sku_id'])
                ->where('good_id', $data['good_id'])
                ->value('num');

            //判断库存是否足够
            if ($good_num < $data['num'] || $data['num'] <= 0 || $good_num <= $num) {
                $msg['error_id'] = '1-08-02-0001';
                $msg['msg'] = '添加失败，库存不够或数量有误';
                return $this->errorJson($msg);
            }

            $data['good_orignal_price'] = $good['data']['original_price'];

            $data['good_price'] = $good_sku['data']['price'];

            $cart_good = Db::name('cart')
                ->field('num')
                ->where('account_id', $data['account_id'])
                ->where('sku_id', $data['sku_id'])
                ->where('good_id', $data['good_id'])
                ->find();
        }


        if ($cart_good) {
            //商品存在购物车中,数量直接累加
            $data['num'] = $data['num'] + $cart_good['num'];
            $data['total_orignal_price'] = $data['good_orignal_price'] * $data['num'];
            $data['total_sales_price'] = $data['good_price'] * $data['num'];
            $data['create_time'] = time();
            $data['sys_sign'] = SYSSIGN;
            if (!isset($data['sku_id'])) {
                $result = Db::name('cart')
                    ->where('account_id', $data['account_id'])
                    ->where('good_id', $data['good_id'])
                    ->strict(false)
                    ->update($data);
            } else {
                $result = Db::name('cart')
                    ->where('account_id', $data['account_id'])
                    ->where('good_id', $data['good_id'])
                    ->where('sku_id', $data['sku_id'])
                    ->strict(false)
                    ->update($data);
            }

        } else {
            $data['total_orignal_price'] = $data['good_orignal_price'] * $data['num'];
            $data['total_sales_price'] = $data['good_price'] * $data['num'];
            $data['create_time'] = time();
            $data['sys_sign'] = SYSSIGN;
            $result = Db::name('cart')
                ->strict(false)
                ->insert($data);
        }


        if ($result) {
            $msg['msg'] = '添加成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-08-02-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除购物车中的商品
     * @description 接口说明
     * @author 李锐志
     * @url /order/front_cart/del_good
     * @method ~
     * @param name:token mobile type:int require:1 desc:token
     * @param name:good_id mobile type:int require:1 desc:商品id
     * @param name:sku_id mobile type:int require:1 desc:skuid
     */
    public function del_good()
    {

        $data = input();

        $data['account_id'] = UID;

        if (!isset($data['sku_id'])) {
            $result = Db::name('cart')
                ->where('account_id', $data['account_id'])
                ->where('good_id', $data['good_id'])
                ->delete();
        } else {
            $result = Db::name('cart')
                ->where('account_id', $data['account_id'])
                ->where('sku_id', $data['sku_id'])
                ->where('good_id', $data['good_id'])
                ->delete();
        }

        if ($result) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-08-02-0003';
            $msg['msg'] = '删除失败，该商品没有在购物车中';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 清空购物车
     * @description 接口说明
     * @author 李锐志
     * @url /order/front_cart/del_good_all
     * @method ~
     * @param name:token mobile type:int require:1 desc:token
     */
    public function del_good_all()
    {
        $data = input();
        $data['account_id'] = UID;

        $result = Db::name('cart')
            ->where('account_id', $data['account_id'])
            ->delete();

        if ($result) {
            $msg['msg'] = '清空成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-08-02-0004';
            $msg['msg'] = '清空失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查看购物车中的商品
     * @description 接口说明
     * @author 李锐志
     * @url /order/front_cart/query_good
     * @method ~
     * @param name:token mobile type:int require:1 desc:token
     */
    public function query_good()
    {
        $data = input();
        $data['account_id'] = UID;

        $cart = Db::name('cart')
            ->where('account_id',$data['account_id'])
            ->select();



        dump($cart);
        exit();
    }
}
